// component/dateCalendar/dateCalendar.js

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        start: {
            type: String,
        },
        end: {
            type: String,
        },
        time: {
            type: String
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        calendar: [null, null, null],
        year: 0,
        month: 0,
        index: 0,
        init: false,
        select: null,
        startRange: null,
        endRange: null,
        dateMap: new Map(),
        today: null
    },
    observers: {
        "start": function (start) {
            this.setStartRange()
            if (this.data.init)
                this.init();
        },
        "end": function (end) {
            this.setEndRange()
            if (this.data.init)
                this.init();
        },
        "time": function (time) {
            const date = this.getDate(time);
            this.setData({
                select: {
                    year: date.getFullYear(),
                    month: date.getMonth() + 1,
                    date: date.getDate()
                }
            })
        }
    },
    lifetimes: {
        ready() {
            const targetDate = this.data.time ? this.getDate(this.data.time) : new Date();
            const today=new Date();
            if (this.data.start && !this.data.startRange) this.setStartRange();
            if (this.data.end && !this.data.endRange) this.setEndRange();
            this.data.dateMap = new Map();
            this.setData({
                calendar: [
                    this.configureMonth(targetDate.getFullYear(), targetDate.getMonth() + 1),
                    this.configureMonth(targetDate.getFullYear(), targetDate.getMonth() + 2),
                    this.configureMonth(targetDate.getFullYear(), targetDate.getMonth()),
                ],
                year: targetDate.getFullYear(),
                month: targetDate.getMonth() + 1,
                init: true,
                today: {
                    year: today.getFullYear(), month: today.getMonth() + 1, date: today.getDate()
                }
            })
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        setStartRange() {
            if (!this.data.start) return;
            this.data.dateMap = new Map();
            this.setData({
                startRange: (this.getDate(this.data.start)).getTime()
            });
        },
        setEndRange() {
            if (!this.data.end) return;
            this.data.dateMap = new Map();
            this.setData({
                endRange: (this.getDate(this.data.end)).getTime()
            });
        },
        getDate(str) {
            if (typeof str == 'string') {
                const tempArr = str.split(/[-/\\_]/);
                return new Date(tempArr[0], tempArr[1] - 1, tempArr[2])
            } else {
                return new Date(str)
            }
        },
        dateClick(e) {
            const lastSelect = { ...this.data.select };
            this.setData({
                select: {
                    year: this.data.year,
                    month: this.data.month,
                    date: e.target.dataset.date,
                    warn: e.target.dataset.warn
                }
            })
            if (e.target.dataset.warn) {
                setTimeout(() => {
                    this.setData({
                        select: lastSelect
                    })
                }, 500)
            } else {
                this.triggerEvent('select', {
                    ...this.data.select
                })
            }
        },
        onswiper(e) {
            // const index = e.detail.current;
            this.data.index = e.detail.current;
            this.init();
        },
        init() {
            const {
                year,
                month
            } = this.data.calendar[this.data.index];
            this.data.calendar[(this.data.index + 2) % 3] = this.configureMonth(year, month - 1);
            this.data.calendar[(this.data.index + 1) % 3] = this.configureMonth(year, month + 1);
            this.data.calendar[this.data.index] = this.configureMonth(year, month);

            this.setData({
                year,
                month,
                calendar: this.data.calendar
            })
        },
        configureMonth(iYear, iMonth) {
            const currentDate = new Date(iYear, iMonth - 1);
            const [year, month] = [currentDate.getFullYear(), currentDate.getMonth() + 1]
            if (this.data.dateMap.has(`${year}-${month}`)) {
                return this.data.dateMap.get(`${year}-${month}`)
            }
            const dateList = [];
            let dayNum;
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    dayNum = 31;
                    break;
                case 2:
                    //判断是否是闰年
                    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 && year % 3200 != 0)) {
                        //闰年
                        if (month == 2) dayNum = 29;
                    } else {
                        //平年
                        if (month == 2) dayNum = 28;
                    }
                    break;
                default:
                    dayNum = 30;
            }
            let start = this.data.startRange,
                end = this.data.endRange;
            for (let day = 1, j = 0; day <= dayNum; day++) {
                const tempDate = new Date(year, month - 1, day);
                const tempTime = tempDate.getTime();
                const realDay = tempDate.getDay();
                let warn = false;
                if ((start && tempTime < start) || (end && tempTime > end)) {
                    warn = true;
                }
                if (!dateList[j]) dateList[j] = [];

                dateList[j].push({
                    day: realDay,
                    date: tempDate.getDate(),
                    warn
                })
                if (realDay == 6 && day !== 0) j++;
            }
            const dateData = {
                year,
                month,
                date: dateList
            }
            this.data.dateMap.set(`${year}-${month}`, dateData)
            return dateData
        }
    }
})